<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-11-17-0017
 * Time: 10:01
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-9-23
 * Time: 下午2:13
 */

class NewsModel extends BaseModel
{
    private $_table;
    protected $_name = 'news';
    protected $_isbug = false;

    const Cache_Count_Key = "newscount";
    const Cache_List_Key = "newslist";
    const Cache_Detail_Key = "newsdetali";
    const Cache_Index_NEWS_Key = "INDEX_NEWS";
    const Cache_Index_NEWSChina_Key = "INDEX_NEWS_CHINA";
    const Cache_browse_Key = "browse";
    const Cache_newstitle_Key = "newstitle";
    const Cache_title_key="title";
    /**
     * get Cache Key
     * @param $module
     * @return string
     */
    private function _getContentCacheKey($module) {
        return "adv_cache_B_{$module}_content";
    }

    public function __construct() {
        parent::__construct();
    }

    public function NewsList($start=0,$count=0,$field,$where ="1=1",$order ="id DESC"){
        $data = parent::getPageListByWhere($start,$count,$field,$where,$order);
        return $data;
    }

    public function getNewsCount($where){
        $data = parent::getCountByWhere($where);
        return $data;
    }

    public function NewsListById($id,$feild){

        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name,$feild)->where("id={$id}")->__toString ();
        $data= $db->fetchRow($sql);
        return $data;
    }


    /**
     * @param $where
     * @param $limit
     * @return array
     */
    public function getNewsByWhere($where,$limit){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $key    = $this->_getContentCacheKey(self::Cache_Index_NEWS_Key);
        //get  Cache
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }
        $sql = $db->select ()->from ("news_cate",array("id","cate_name","keywords","title","image"))->where($where)->__toString ();
        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        if(!empty($limit)){
            $sql.=" limit {$limit}";
        }
        $data = $db->fetchAll($sql);
        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    public function getNewsChildById($where){
        $key    = $this->_getContentCacheKey(self::Cache_Index_NEWSChina_Key);
        //get  Cache
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }
        $field = array(
            'id','release_time','title','is_release'
        );
        $limit = " 0,7";
        $order = " click_num desc";
        $data =  parent::getCateChildById($where,$field,$order,$limit);

        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    public function Up($id){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name,array("id","title"))->where("id < {$id} and is_release='Y'")
            ->order("id desc")
            ->limit(1)
            ->__toString ();
        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        return $db->fetchRow($sql);
    }

    public function Down($id){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name,array("id","title"))->where("id > {$id} and is_release='Y'")
            ->order("id asc")
            ->limit(1)
            ->__toString ();
        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        return $db->fetchRow($sql);
    }

    public function Browse($id,$click_num){
        $this->_table = new self();
        $db = $this->_table->getAdapter();
        return $db->update("news",$click_num,"id = '{$id}'");
    }

    public function Order(){
            $this->_table = new self ();
            $db = $this->_table->getAdapter ();
            $sql = $db->select ()->from ( $this->_name,array("id","title","date_format(release_time,'%m-%d') as time"))
                ->order("click_num DESC")
                ->limit(10)
                ->__toString ();
            if ($this->_isbug == true) {
                echo  __METHOD__.": ".$sql . "<hr/>";
            }
            return $db->fetchAll($sql);
        }


    public function NewsTpye(){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( "news_cate",array("id","cate_name"))
            ->where("is_show='Y'")
            ->__toString ();
        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        return $db->fetchAll($sql);
    }

    //相似文章查询


    public function OldTitle($id){
        $key = $this->_getContentCacheKey(self::Cache_title_key);
        $data=$this->_cache_handler->load($key);
        if(!empty($data)){
            return $data;
        }
        $data=parent::getListById($id);
        $this->_cache_handler->put($key,$data,300);
        return $data;
    }


    public function getTitleByWhere($where,$field){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name,$field)->where($where)
            ->limit(5)
            ->__toString ();
        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        return $db->fetchAll($sql);
    }

    //这边的是添加的新闻首页
    public function getCateInfo($where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( "news_cate")
            ->where($where)
            ->__toString ();
        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        return $db->fetchRow($sql);
    }

    public function getCateInfoDetail($where,$limit,$order){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( "news")
            ->where($where)
            ->order($order)
            ->limit($limit)
            ->__toString ();
        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        return $db->fetchAll($sql);
    }


}